package scm.erp.core.auth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
        log.info("[前置拦截]:请求地址-{},时间-{}", request.getRequestURI(), LocalDateTime.now());
        LoginSession loginUserInfo = LoginSession
                .resolve(request)
                .setThreadLocal();
        log.info("登录用户信息:登录账户-{};登录用户-{}",
                loginUserInfo.getAccountName(),
                loginUserInfo.getUserName());
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler,
            ModelAndView modelAndView) throws Exception {
        log.info("[后置拦截]:请求地址-{},时间-{}", request.getRequestURI(), LocalDateTime.now());
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler,
            Exception ex) throws Exception {
        log.info("编译后拦截");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
